// pages/withdraw/withdraw.js
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    balance:0,
    sum:0,
    rule:false,
    maxmoney: 10,
    info:'',
    phone:'',
  },
  getPhoneNumber: function (e) {
    var that = this;
    var phone = app.siteinfo.userinfo.phone
    console.log('hasphone')
    wx.checkSession({
      success: function () {

        var ency = encodeURIComponent(e.detail.encryptedData);
        var iv = e.detail.iv;
        var sessionk = wx.getStorageSync('sessionkey');

        if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {

        } else {//同意授权
          wx.request({
            method: "GET",
            url: app.siteinfo.rootpath + 'getphonenumber',
            data: {
              uid: app.siteinfo.userinfo.id,
              encryptedData: ency,
              iv: iv,
              sessionkey: sessionk
            },
            success: (res) => {
              console.log(res);
              var phone = res.data.phone;
              app.siteinfo.userinfo.phone = phone
              console.log(phone);
              that.setData({
                hasphone: true
              })
              wx.navigateTo({
                url: '/pages/editCard/editCard?id=' + that.data.id,
              })
            }, fail: function (res) {
              console.log("解密失败~~~~~~~~~~~~~");
              console.log(res);
            }
          });
        }
      },
      fail: function () {
        console.log("session_key 已经失效，需要重新执行登录流程");
        that.wxlogin(); //重新登录
      }
    });
  },
  withdraw: function () {
    this.setData({
      sum: this.data.balance
    })
  }, 
  inputHandle: function (e) {
    console.log(e.detail.value)
    var that = this
    
    that.setData({
      sum: e.detail.value
    })
  },
  ruleHandle: function () {
    this.setData({
      rule: !this.data.rule
    })
  }, 
  withdrawDetails: function () {
    wx.navigateTo({
      url: '/pages/withdrawDetails/withdrawDetails',
    })
  }, 
  orderwithdraw:function(){
    var that = this;
    var money = that.data.sum
    var totalmoney = that.data.balance
    var maxmoney = that.data.maxmoney;
    if (totalmoney < maxmoney){
      wx.showToast({
        title: '不满足提现条件',
        icon:"none"
      })
    } else if (money < 1 || !money || money > totalmoney){
      wx.showToast({
        title: '请正确输入提现金额',
        icon: "none"
      })
      that.setData({
        num:0
      })
    }else{
      wx.request({
        url: app.siteinfo.rootpath+'tixian',
        data:{
          uid:app.siteinfo.userinfo.id,
          money:money
        },
        success:function(res){
          if(res.data.code == 200){
            wx.showLoading({
              title: '提现处理中',
            })
            wx.request({
              url: app.siteinfo.rootpath + 'paytixian',
              data: {
                uid: app.siteinfo.userinfo.id,
                id: res.data.id
              },
              success: function (res2) {
                if(res2.data.code == 200){
                  wx.showToast({
                    title: '提现成功',
                  })
                  setTimeout(function(){
                    wx.switchTab({
                      url: '/pages/mine/mine',
                    })
                  },1500)
                }else{
                  wx.showToast({
                    title: '提现失败',
                    icon:"none"
                  })
                }
                
              },
              complete:function(){
                wx.hideLoading();
              }
            })
            
          }else{
            wx.showToast({
              title: res.data.msg,
              icon:"none"
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      balance:app.siteinfo.userinfo.money,
      phone: app.siteinfo.userinfo.phone
    })
    wx.request({
      url: app.siteinfo.rootpath+'getwithdraw',
      success:function(res){
        var article = res.data.info.detail
        WxParse.wxParse('article', 'html', article, that, 5);
        that.setData({
          info: article,
          maxmoney: res.data.info.maxmoney,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})